package com.topica.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Util {

	public static final long NO_SDCARD = -1;
	public static final long SDCARD_CANNOT_WRITE = -2;

	@SuppressLint("NewApi")
	public static String displayAudioTime(int milisec) {
		return String.format(
				"%02d:%02d",
				TimeUnit.MILLISECONDS.toMinutes(milisec),
				TimeUnit.MILLISECONDS.toSeconds(milisec)
						- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
								.toMinutes(milisec)));
	}

	@SuppressLint("NewApi")
	public static String displayCountdown(long millis) {
		return String.format(
				"%02d:%02d:%02d",
				TimeUnit.MILLISECONDS.toHours(millis),
				TimeUnit.MILLISECONDS.toMinutes(millis)
						- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
								.toHours(millis)),
				TimeUnit.MILLISECONDS.toSeconds(millis)
						- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
								.toMinutes(millis)));
	}

	public static String getFileName(String url) {
		String fileName = url.substring(url.lastIndexOf("/") + 1, url.length());
		return fileName;
	}

	public static long convertDateToMinisecond(String date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		formatter.setLenient(false);
		Date oldDate;
		long oldMillis = -1;
		try {
			oldDate = formatter.parse(date);
			oldMillis = oldDate.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return oldMillis;
	}

	public static String convertCurrenttimeToDate(long milliSeconds) {
		String dateFormat = "yyyy-MM-dd HH:mm:ss";
		DateFormat formatter = new SimpleDateFormat(dateFormat);
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(milliSeconds);
		return formatter.format(calendar.getTime());
	}

	public static boolean isSdCardWrittenable() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return true;
		}
		return false;
	}

	public static long getAvailableStorage() {
		if (!isSdCardWrittenable()) {
			return SDCARD_CANNOT_WRITE;
		}
		if (!isSDCardPresent()) {
			return NO_SDCARD;
		}
		String storageDirectory = null;
		storageDirectory = Environment.getExternalStorageDirectory().toString();
		Log.v(null, "getAvailableStorage. storageDirectory : "
				+ storageDirectory);

		try {
			StatFs stat = new StatFs(storageDirectory);
			long avaliableSize = ((long) stat.getAvailableBlocks() * (long) stat
					.getBlockSize());
			Log.v(null, "getAvailableStorage. avaliableSize : " + avaliableSize);
			return avaliableSize;
		} catch (RuntimeException ex) {
			Log.e(null, "getAvailableStorage - exception. return 0");
			return 0;
		}
	}

	public static boolean checkAvailableStorage(long threshold) {
		if (getAvailableStorage() < threshold) {
			return false;
		}

		return true;
	}

	public static boolean isSDCardPresent() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}

	public static boolean isConnectingToInternet(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null)
				for (int i = 0; i < info.length; i++)
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}

		}
		return false;
	}

	public static boolean isCurrentTimeBetween(String startDate, String endDate)
			throws ParseException {
		DateFormat dateFormar = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date now = new Date();
		String str = dateFormar.format(now);
		System.out.println("start="+startDate);
		System.out.println("now=" + str);
		System.out.println("end="+endDate);
		System.out.println(dateFormar.parse(startDate).before(now) + " " + dateFormar.parse(endDate).after(now));
		return (dateFormar.parse(startDate).before(now) && dateFormar.parse(endDate).after(now));
	}
	
	public static boolean hasBook(String path){
		File f = new File("/mnt/sdcard/topica/" + path);
		if (!f.exists()) {
			f.mkdirs();
		}
		File[] listFile = f.listFiles();
		if(null != listFile && listFile.length > 0){
			return true;
		} else {
			return false;
		}
	}
	
	public static boolean hasVideo(String path){
		if (null == path || path.equals("")){
			return false;
		} else {
			return true;
		}
	}
	
	public static boolean hasAudio(String path){
		if(path.endsWith("mp3")){
			return true;
		} else {
			return false;
		}
	}
	
}
